package org.xteam.sled.semantic;

import java.util.HashMap;
import java.util.Map;

import org.xteam.parser.runtime.Span;
import org.xteam.sled.ast.AbstractPattern;
import org.xteam.sled.ast.AndPattern;
import org.xteam.sled.ast.DefaultSledVisitor;
import org.xteam.sled.ast.LabelPattern;
import org.xteam.sled.ast.OrPattern;
import org.xteam.sled.ast.PrefixPattern;
import org.xteam.sled.ast.SequentPattern;
import org.xteam.sled.ast.SuffixPattern;


public class LabelCollector extends DefaultSledVisitor {

	private Map<String, Span> labels = new HashMap<String, Span>();

	public LabelCollector() {
	}

	public Map<String, Span> collect(AbstractPattern pattern) {
		if (pattern != null)
			pattern.visit(this);
		return labels;
	}
	
	public void visitOrPattern(OrPattern aOrPattern) {
		aOrPattern.getLeft().visit(this);
		aOrPattern.getRight().visit(this);
	}
		
	public void visitSequentPattern(SequentPattern aSequentPattern) {
		aSequentPattern.getLeft().visit(this);
		aSequentPattern.getRight().visit(this);
	}
		
	public void visitLabelPattern(LabelPattern aLabelPattern) {
		labels.put(aLabelPattern.getIdent().getName(), aLabelPattern.getIdent().span());
		aLabelPattern.getExpr().visit(this);
	}
			
	public void visitAndPattern(AndPattern aAndPattern) {
		aAndPattern.getLeft().visit(this);
		aAndPattern.getRight().visit(this);
	}
		
	public void visitPrefixPattern(PrefixPattern aPrefixPattern) {
		aPrefixPattern.getExpr().visit(this);
	}
		
	public void visitSuffixPattern(SuffixPattern aSuffixPattern) {
		aSuffixPattern.getExpr().visit(this);
	}
	
}
